﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Area
{
    public class RegisterAreaModel
    {
        [DisplayName("Nombre del Área *")]
        [Required(ErrorMessage = "El nombre del proceso es requerido")]
        [StringLength(50, ErrorMessage = "El nombre debe ser máximo de 50 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El nombre debe ser solo letras o números")]
        public string Name { get; set; }

        [DisplayName("Jefatura/Gerencia *")]
        [Required(ErrorMessage="Una jefatura/gerencia es necesaria")]
        public int Management { get; set; }

        [DisplayName("Proceso *")]
        [Required(ErrorMessage = "El proceso es necesario")]
        public int Process { get; set; }

        public IEnumerable<SelectListItem> Managements
        {
            get
            {
                return managementList;
            }
        }

        public IEnumerable<SelectListItem> Processes
        {
            get
            {
                return processList;
            }
        }

        private List<SelectListItem> managementList;
        private List<SelectListItem> processList;

        riskanalysisEntities entities = new riskanalysisEntities();

        public RegisterAreaModel()
        {
            FillList();
        }

        private void FillList()
        {
            managementList = new List<SelectListItem>();
            
            var queryManagements = from m in entities.managements
                                   where m.enabled
                                   select m;

            foreach (management item in queryManagements)
            {
                managementList.Add(
                    new SelectListItem
                    {
                        Text = item.name,
                        Value = item.managementId.ToString()
                    });
            }
            processList = FillProcessList(queryManagements.First().managementId);
        }

        public static List<SelectListItem> FillProcessList(int managementId)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            riskanalysisEntities entities = new riskanalysisEntities();

            var queryProcesses = from p in entities.processes
                                 where p.enabled && p.managementId == managementId
                                 select p;

            foreach (process item in queryProcesses)
            {
                list.Add(
                    new SelectListItem
                    {
                        Text = item.name,
                        Value = item.processId.ToString()
                    });
            }

            return list;
        }

        public void Save()
        {
            var query = from a in entities.areas
                        where a.enabled && a.processId == Process && a.name.ToLower().Equals(Name.ToLower())
                        select a;

            if (query.Count() >= 1)
                throw new RegisterAreaException("Ya existe un area con el mismo nombre en el proceso seleccionado");

            entities.AddToareas(
                new area
                {
                    name = Name,
                    processId = Process,
                    enabled = true
                });
            entities.SaveChanges();           
        }
    }
}